package com.example.music.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.music.dao.SongListMapper;
import com.example.music.dto.SongListStyleDto;
import com.example.music.entity.Consumer;
import com.example.music.entity.Song;
import com.example.music.entity.SongList;
import com.example.music.service.SongListService;
import com.example.music.utils.AjaxJson;
import com.example.music.utils.ContentValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SongListServiceImpl extends BaseServiceImpl<SongList, SongListMapper> implements SongListService {
    @Autowired
    SongListMapper songListMapper;

    @Override
    protected SongListMapper getMapper() {
        return songListMapper;
    }

    @Override
    public boolean updateSongList(SongList songList) {
        int i = songListMapper.updateById(songList);
        if (i<1){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public List<SongList> songListOfTitle(String title) {
        QueryWrapper<SongList> songListQueryWrapper = new QueryWrapper<>();
        QueryWrapper<SongList> title1 = songListQueryWrapper.like("title", title);
        List<SongList> songLists = songListMapper.selectList(title1);
        return songLists;
    }

    @Override
    public List<SongList> songListOfStyle(String style) {
        QueryWrapper<SongList> songListQueryWrapper = new QueryWrapper<>();
        QueryWrapper<SongList> title1 = songListQueryWrapper.like("style", style);
        List<SongList> songLists = songListMapper.selectList(title1);
        return songLists;
    }

    @Override
    public AjaxJson updateSongListPic(Integer id, MultipartFile file) {
        if (file.isEmpty()){
            return AjaxJson.getError();
        }
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        String filePath = ContentValue.userDir+ContentValue.Slash+"img"+
                ContentValue.Slash+"songListPic";
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }
        File dest = new File(filePath+ContentValue.Slash+fileName);
        String mysqlDir = ContentValue.Slash+"img"+ContentValue.Slash+"songListPic"+ContentValue.Slash+fileName;
//        String mysqlDir = "/img/songPic/"+fileName;
        try {
            file.transferTo(dest);
            SongList song = new SongList();
            song.setId(id.longValue());
            song.setPic(mysqlDir);
            Integer update = songListMapper.updateById(song);
            if (update == 1){
                return AjaxJson.getSuccess();
            }
            return AjaxJson.getError();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AjaxJson songStyle() {
        List<SongList> songLists = songListMapper.selectList(null);
        Map<String, List<SongList>> collect = songLists.parallelStream().collect(Collectors.groupingBy(SongList::getStyle));
        ArrayList<SongListStyleDto> arrayList = new ArrayList<>();
        collect.forEach((key,value) -> {
            arrayList.add(new SongListStyleDto(key,value.size()));
        });
        List<SongListStyleDto> collect1 = arrayList.stream().sorted(Comparator.comparing(SongListStyleDto::get总数).reversed()).limit(7).collect(Collectors.toList());
        return AjaxJson.getSuccessData(collect1);
    }
}
